Check failed shape[i] >= 0 (-1 vs. 0)错误

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

在使用Caffe模型进行图像分类时,通常使用的图像大小为224 * 224,如果改变输入图像的大小,有时候会碰到Check failed: shape[i] >= 0 (-1 vs. 0)错误。

  • 背景

使用ResNet-50进行图像分类训练,输入图像的大小为160 *160

  • 错误
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
I1013 10:53:03.395526 76959 net.cpp:129] Top shape: 16 2048 5 5 (819200)
I1013 10:53:03.395531 76959 net.cpp:137] Memory required for data: 1889075264
I1013 10:53:03.395539 76959 layer_factory.hpp:77] Creating layer res5c_relu
I1013 10:53:03.395553 76959 net.cpp:84] Creating Layer res5c_relu
I1013 10:53:03.395567 76959 net.cpp:406] res5c_relu <- res5c
I1013 10:53:03.395594 76959 net.cpp:367] res5c_relu -> res5c (in-place)
I1013 10:53:03.395841 76959 net.cpp:122] Setting up res5c_relu
I1013 10:53:03.395855 76959 net.cpp:129] Top shape: 16 2048 5 5 (819200)
I1013 10:53:03.395861 76959 net.cpp:137] Memory required for data: 1892352064
I1013 10:53:03.395869 76959 layer_factory.hpp:77] Creating layer pool5
I1013 10:53:03.395900 76959 net.cpp:84] Creating Layer pool5
I1013 10:53:03.395908 76959 net.cpp:406] pool5 <- res5c
I1013 10:53:03.395931 76959 net.cpp:380] pool5 -> pool5
F1013 10:53:03.396807 76959 blob.cpp:32] Check failed: shape[i] >= 0 (-1 vs. 0)
*** Check failure stack trace: ***
@ 0x7fa7eef89e6d (unknown)
@ 0x7fa7eef8bced (unknown)
@ 0x7fa7eef89a5c (unknown)
@ 0x7fa7eef8c63e (unknown)
@ 0x7fa7f6141829 caffe::Blob<>::Reshape()
@ 0x7fa7f6141581 caffe::Blob<>::Reshape()
@ 0x7fa7f6200958 caffe::PoolingLayer<>::Reshape()
@ 0x7fa7f61b14d8 caffe::CuDNNPoolingLayer<>::Reshape()
@ 0x7fa7f6167d29 caffe::Layer<>::SetUp()
@ 0x7fa7f6240213 caffe::Net<>::Init()
@ 0x7fa7f623e7ea caffe::Net<>::Net()
@ 0x7fa7f62711e1 caffe::Solver<>::InitTrainNet()
@ 0x7fa7f6270a75 caffe::Solver<>::Init()
@ 0x7fa7f62705a7 caffe::Solver<>::Solver()
@ 0x7fa7f627dc66 caffe::SGDSolver<>::SGDSolver()
@ 0x7fa7f6287ab1 caffe::Creator_SGDSolver<>()
@ 0x4215f5 caffe::SolverRegistry<>::CreateSolver()
@ 0x41bdce train()
@ 0x41e167 main
@ 0x7fa7e20eeb35 __libc_start_main
@ 0x41aa69 (unknown)
  • 解决方案

错误日志中可以看出问题出在pool5层,它的输入为Top shape: 16 2048 5 5 (819200),即16 * 2048 * 5 * 516batch_size2048channel数量,即kernel个数,feature map大小为5 * 5,在prototxt中找到pool5层,如下:

1
2
3
4
5
6
7
8
9
10
11
layer {
bottom: "res5c"
top: "pool5"
name: "pool5"
type: "Pooling"
pooling_param {
kernel_size: 7
stride: 1
pool: AVE
}
}

kernel size为7,比5大,因此将7改为5,重新训练,没问题。

如果有收获,可以请我喝杯咖啡!